package com.dhsc.service.impl.xtqxgl;

import com.dhsc.domain.ywzx.xtqxgl.BqqxshVo;
import com.dhsc.domain.yypt.LayuiTableVo;
import com.dhsc.domain.yypt.ResponseObject;
import com.dhsc.domain.yypt.sys.TXtdmVo;
import com.dhsc.domain.yypt.yhzx.TJgxxVo;
import com.dhsc.exception.MyException;
import com.dhsc.local.dao.common.ICommonDao;
import com.dhsc.local.dao.xtqxgl.IBqqxshDao;
import com.dhsc.local.dao.yypt.IYhzxDao;
import com.dhsc.service.xtqxgl.IBqqxshService;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: BqqxshServiceImpl
 * @Description: 标签权限审核
 * @date: 2023/11/28 14:29
 * @author: gx
 */
@Service
@Slf4j
public class BqqxshServiceImpl implements IBqqxshService {

    @Autowired
    private IBqqxshDao bqqxshDao;

    @Autowired
    private IYhzxDao yhzxDao;

    @Autowired
    private ICommonDao commonDao;

    @Override
    public LayuiTableVo queryBqqxsh(BqqxshVo bqqxshVo) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(bqqxshVo.getPage(), bqqxshVo.getLimit());
            List<BqqxshVo> list = bqqxshDao.queryBqqxsh(bqqxshVo);
            PageInfo<BqqxshVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
        }catch (Exception e){
            log.error("查询标签权限审核信息出错",e);
            throw new MyException("查询标签权限审核信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject queryBqqxshByZt(BqqxshVo bqqxshVo) {
        ResponseObject obj = new ResponseObject();
        try {
            //查询字典
            TXtdmVo txtdmVo = new TXtdmVo();
            txtdmVo.setFldmstr("TBQ_XTLB");
            List<TXtdmVo> xtdmVos = commonDao.queryQsTXtdmByFldmStr(txtdmVo);

            TJgxxVo jgxxVo = new TJgxxVo();
            jgxxVo.setJgdm(bqqxshVo.getJgdm());
            List<TJgxxVo> jgxxVos = yhzxDao.queryTJgxxKhqxAll(jgxxVo);
            List<String> list = jgxxVos.stream().map(TJgxxVo::getJgdm).collect(Collectors.toList());
            String s = list.stream().collect(Collectors.joining(","));
            bqqxshVo.setJgdm(s);
            PageMethod.startPage(bqqxshVo.getStart(),bqqxshVo.getPagesize());
            List<BqqxshVo> bqqxshVos = bqqxshDao.queryBqqxsh(bqqxshVo);
            PageInfo<BqqxshVo> pageInfo = new PageInfo<>(bqqxshVos);
            for (int i = 0; i < bqqxshVos.size(); i++) {
                BqqxshVo bqqxsh = bqqxshVos.get(i);
                String xtlxstr = "";
                for (int j = 0; j < xtdmVos.size(); j++) {
                    TXtdmVo xtdmVo = xtdmVos.get(j);
                    if (xtdmVo.getValue().equals(bqqxsh.getXtlx())){
                        xtlxstr = xtdmVo.getName();
                        continue;
                    }
                }
                if (!xtlxstr.equals("")){
                    bqqxsh.setXtlxstr(xtlxstr);
                }else{
                    if (bqqxsh.getXtlx().equals("bq")){
                        xtlxstr = "标签调整";
                    }
                    bqqxsh.setXtlxstr(xtlxstr);
                }
            }
            obj.setData(bqqxshVos);
            obj.setTotal(pageInfo.getTotal());
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("查询标签权限审核信息出错",e);
            throw new MyException("查询标签权限审核信息出错",e);
        }
        return obj;
    }

    @Transactional("localTransactionManager")
    @Override
    public ResponseObject saveBqqxsh(BqqxshVo bqqxshVo) {
        ResponseObject obj = new ResponseObject();
        try {
            //判断xtlx是否为bq
            if (bqqxshVo.getXtlx().equals("bq")){
                //如果是，则继续判断审核标志，为1则是审核通过
                if (bqqxshVo.getShbz().equals("1")){
                    bqqxshDao.updateIsbqPass1(bqqxshVo);
                    bqqxshDao.updateIsbqPass2(bqqxshVo);
                }else{
                    //不为1则是审核退回
                    bqqxshDao.updateIsbqReturn(bqqxshVo);
                }
            }else{
                //如果不是bq，则执行该方法
                bqqxshDao.updateIsNotbq(bqqxshVo);
            }
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("保存标签权限审核信息出错",e);
            throw new MyException("保存标签权限审核信息出错",e);
        }
        return obj;
    }
}
